package com.power.authservice.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.power.authservice.domain.LoinSysUser;
import com.power.authservice.mapper.LoginSysUserMapper;
import com.power.authservice.model.SecurityUser;
import com.power.authservice.strategy.LoginStrategy;
import com.power.constant.AuthConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 商城管理系统用户登录具体实现
 */
@Slf4j
@Service(AuthConstants.SYS_USER_LOGIN)
public class SysUserLoginStrategy  implements LoginStrategy {
    @Autowired
    private LoginSysUserMapper loginSysUserMapper;
    @Override
    public UserDetails realLogin(String username) {
        LoinSysUser loinSysUser = loginSysUserMapper.selectOne(new LambdaQueryWrapper<LoinSysUser>().eq(LoinSysUser::getUsername, username));
        if (loinSysUser != null) {
            Set<String> perms = loginSysUserMapper.selectPermsByUserId(loinSysUser.getUserId());
            SecurityUser user=new SecurityUser();
            user.setUserId(loinSysUser.getUserId());
            user.setPassword(loinSysUser.getPassword());
            user.setShopId(loinSysUser.getShopId());
            user.setStatus(loinSysUser.getStatus());
            user.setLoginType(AuthConstants.SYS_USER_LOGIN);
            if (perms != null&& perms.size() > 0)
            {
                user.setPerms(perms);
            }
            return user;
        }

        return null;
    }
}
